package com.blt.car.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.utils.ActivityHelper;

import java.util.ArrayList;
import java.util.List;

import blt.app_core.api.base.CoreManager;
import blt.app_core.api.car.CarApi;
import blt.app_core.api.car.bean.Car;
import blt.app_core.api.car.bean.CarListVo;
import blt.app_core.api.message.bean.MessageInfo;

/**
 * Created by xgp on 2017/6/19.
 */

public class CarListAdapter extends BaseAdapter {

    private Context context;

    private LayoutInflater inflater;
    private List<Car> carList;

    public CarListAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        carList = new ArrayList<>();
    }

    public List<Car> getCarList() {
        if (carList != null) {
            return carList;
        }
        return null;
    }

    @Override
    public int getCount() {
        return carList.size();
    }

    @Override
    public Car getItem(int position) {
        return carList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        CarHolder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_car_list, parent, false);
            holder = new CarHolder();
            holder.carNo = (TextView) view.findViewById(R.id.item_car_license);
            holder.status = (TextView) view.findViewById(R.id.item_car_status);
            view.setTag(holder);
        } else {
            holder = (CarHolder) view.getTag();
        }

        holder.carNo.setText(carList.get(position).carPlateNumber);
        int status = carList.get(position).carStatus;
        if (status == 0) {
            holder.status.setText("待审核");
            holder.status.setTextColor(context.getResources().getColor(R.color.red));
        } else if (status == 1) {
            holder.status.setText("已审核");
            holder.status.setTextColor(context.getResources().getColor(R.color.text_color_blue));
        } else if (status == 2) {
            holder.status.setText("未通过");
            holder.status.setTextColor(context.getResources().getColor(R.color.text_color_orange));
        }
        return view;
    }


    public void addData(List<Car> data) {
        carList.clear();
        carList.addAll(data);
        notifyDataSetChanged();
    }

    public void removeCar(int position) {
        int carId = carList.get(position).carId;
        carList.remove(position);
        CoreManager.getCore(CarApi.class).deleteCar(carId);
        notifyDataSetChanged();
    }

    class CarHolder {
        TextView carNo;
        TextView status;
    }
}
